// pages/product/product_detail.js
const app = getApp();
const util = require('../../utils/util.js')

import {
  GoodsModels
} from "../../utils/models/goods/Goods-models";
var goodsModels = new GoodsModels();
Page({

  /**
   * 页面的初始数据
   */
  data: {
    navbarHeight: app.globalData.navbarHeight,
    screenHeight: '',
    height: '',
    titleName: '',
    isHeight: false,
    navBgColor: "white",
    isLeft: true,
    autoplay: true,
    indicatorDots: true,
    interval: 5000,
    duration: 500,
    current: 0,
    detail:{},
    headimgList:[],
    business:{},
    recommendGoods:[],
    shopTypeList: [{
      title: '杰少棒棒的',
      images: '/images/test/product.png',
      now_price: '228',
      old_price: '399',
    }, {
      title: '杰少好犀利',
      images: '/images/test/product2.png',
      now_price: '318',
      old_price: '288',
    }, {
      title: '杰少好犀利',
      images: '/images/test/i3.png',
      now_price: '308',
      old_price: '499',
    }],
    isEnd: false,
    isMask: false,
    isType: false,
    animationType: '',
    animationEnd: '',
    endModal: false, //已结束推荐弹框
    typeModal: false, //选择商品分类弹框
    more:[],
    commentList:[],
    buyRecordList:[],
    idx:1,
    invite_id:0,
  },

  // 回到首页
  onHome: function() {
    wx.switchTab({
      url: '../index/index',
    })
  },

  // 购物车
  onShopcart: function() {
    wx.navigateTo({
      url: '../shopping_cart/shopping_cart',
    })
   },

  // 分享
  onShare: function() {
   wx.showModal({
     title: '分享'
   })
  },

  // 关闭已结束弹框
  closeOver: function() {
    util.showModal(this, 'isMask', 'animationEnd', '900rpx', '0')
  },
  
  getGoodDetail:function(id){
    let param = {
      'id':id
    }
    let that = this;
    
    goodsModels.getGoodsDetail(param,function(res){
      console.log(res);
      let data = res.data;
      if (data.detail.stock <= 0 && data.recommend_list.length > 0){
		    util.showModal(that, 'isMask', 'animationEnd', '900rpx', '0')
      }

      that.setData({
        swiperList:data.detail.images,
        detail:data.detail,
        headimgList: data.headimglist,
        business:data.business,
        recommendGoods: data.recommend_list,
        isEnd: data.detail.stock > 0 ? false : true ,
        recommend_list: data.recommend_list,
        more:data.more,
        commentList: data.comment_list,
        buyRecordList: data.buyRecordList
      })
    })
  },

  // 点击立即购买
  goBuy: function() {
    var invite_id = this.data.invite_id;
    if (this.data.detail.isType == true) {
      util.showModal(this, 'isType', 'animationType', '900rpx', '0')
    }else{
      wx.navigateTo({
        url: '../orders/orders_confirm?id=' + this.data.detail.id+'&idx=0&invite_id='+invite_id,
      })
    }
  },
  // 此处为弹框后点击立即购买
  goBuy2: function(e) {
    var invite_id = this.data.invite_id;
    var idx = e.currentTarget.dataset.idx;
    wx.navigateTo({
      url: '../orders/orders_confirm?id=' + this.data.detail.id + '&idx='+idx+'&invite_id='+invite_id,
    })
  },

  // 选择商品类型
  onRadio: function(e) {
    console.log(e);
    let idx = e.currentTarget.dataset.idx;
    this.setData({
      current: e.currentTarget.dataset.index,
      idx:idx
    });
  },

  // 点击遮罩关闭弹框
  closeMask: function(){
    util.showModal(this, 'isMask', 'animationEnd', '900rpx', '0')
  },
  closeType: function(){
    util.showModal(this, 'isType', 'animationType', '900rpx', '0')
  },

  // 拨打手机号码
  phoneCall: function (e) {
    var tel = this.data.business.phone;
    if (tel) {
      wx.makePhoneCall({
        phoneNumber: tel,
        success: function (res) {

        },
        fail: function (res) {
          console.log('拨打失败')
        }
      })
    }

  },

  // 打开地图查看地址
  onLocation: function() {
    var that = this;
    wx.getLocation({
      type: 'gcj02', //返回可以用于wx.openLocation的经纬度
      success(res) {
        const latitude = parseFloat(that.data.business.latitude);
        const longitude = parseFloat(that.data.business.longitude);

        wx.openLocation({
          latitude,
          longitude,
          scale: 18,
          name: that.data.business.title,
          address: that.data.business.address
        })
      }
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    let gid = options.id;
    let invite_id = options.invite_id;
    this.setData({
      'invite_id':invite_id
    });
    // gid = 1;
    this.getGoodDetail(gid);
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})